﻿<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="javascript/jquery-1.8.0.min.js"></script>
    <script src="javascript/DB/tools.js"></script>
    <script src="javascript/DB/dbModel.js"></script>
    <script src="javascript/DB/dbCore.js"></script>
    <script src="javascript/DB/dbSync.js"></script>
    <script>
        function msgObj(name, msg) {
            this._temp = _dbObj;
            this._temp(uuid(), "msg");
            this.name = name;
            this.msg = msg;
            this.c = (new Date()).getTime();
            return this;
        }
        dbModelIndex['msg'] = "c";//order by c
        dbModelColumn['msg'] = ['name', 'msg', 'c'];
        dbModelIndexType['msg'] = "int";
        var mySyncDB = new MySyncDB('testuser', 'lts', '/bmsync/');
        mySyncDB.autoSync(function (newdata) {
            if (newdata.length) {
                var msgs = '';
                for (var i = 0; i < newdata.length; i++) {
                    if (!newdata[i].fromLocal) {
                        var msg = newdata[i].data;
                        msgs += (new Date(msg.c)) + '\r\n' + msg.name + ' :' + msg.msg + '\r\n';
                    }

                }
                jQuery('#msgs').val(msgs + jQuery('#msgs').val());
            }
        });
        jQuery(function () {
            jQuery('#send').click(function () {
                var msg = new msgObj(jQuery('#name').val(), jQuery('#msg').val());
                var msgs = (new Date(msg.c)) + '\r\n' + msg.name + ' :' + msg.msg + '\r\n';
                jQuery('#msgs').val(msgs + jQuery('#msgs').val());
                mySyncDB.writeObj(msg);
            });
        });
    </script>
</head>
<body>
    name:<input id="name" type="text" name="name" value="user" />
    msg:<input id="msg" type="text" name="name" value="" />
    <input id="send" type="button" name="name" value="send" />
    <textarea id="msgs" style="width:100%;height:200px;">
    </textarea>

</body>
</html>
